package com.trace.app.service.impl;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.Category;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.CategoryVo;
import com.trace.app.entity.vo.LabelValueVo;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.CategoryMapper;
import com.trace.app.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (Category)表服务实现类
 *
 * @author lionkliu
 * @since 2024-04-02 12:38:15
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        Page<Category> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<Category> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Category category) {
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        long id = snowflake.nextId();
        category.setKey(String.valueOf(id));
        return save(category) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Category category) {
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(Category::getId, category.getId());
        if (category.getName() != null) {
            updateWrapper.set(Category::getName, category.getName());
        }
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCategory() {
        List<CategoryVo> collect = list().stream().map((category -> {
            CategoryVo categoryVo = new CategoryVo();
            categoryVo.setId(category.getId());
            categoryVo.setLabel(category.getName());
            categoryVo.setKey(category.getKey());
            categoryVo.setChildren(null);
            return categoryVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult getLabelValue() {
        List<LabelValueVo> collect = list().stream().map((category -> {
            LabelValueVo labelValueVo = new LabelValueVo();
            labelValueVo.setValue(String.valueOf(category.getId()));
            labelValueVo.setLabel(category.getName());
            return labelValueVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }
}

